import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class PrismImporter
{
	int numDimensions;
	Scanner scan;

	private ArrayList<double[]> vertexList = new ArrayList<double[]>(0);
	private ArrayList<double[]> orderedVertices = new ArrayList<double[]>(0);
	private ArrayList<double[]> textureList = new ArrayList<double[]>(0);
	private ArrayList<double[]> orderedTextures = new ArrayList<double[]>(0);
	
	private double minX = Double.POSITIVE_INFINITY, minY = Double.POSITIVE_INFINITY, minZ = Double.POSITIVE_INFINITY;
	private double greatestX = Double.NEGATIVE_INFINITY, greatestY = Double.NEGATIVE_INFINITY, greatestZ = Double.NEGATIVE_INFINITY;
	private double sizeX = 0, sizeY = 0, sizeZ = 0;

	public double getVectorForFace(int face, int vertexNum)
	{
		return orderedVertices.get(face)[vertexNum];
	}
	
	public double getTextureForFace(int face, int texNum)
	{
		return orderedTextures.get(face)[texNum];
	}
	
	public int getNumFaces()
	{
		return orderedVertices.size();
	}
	
	public Prism getPrism(boolean isMovable)
	{
		return new Prism(minX, minY, minZ, sizeX, sizeY, sizeZ, isMovable);
	}
	
	public double getOriginalMinX()
	{
		return minX;
	}
	
	public double getOriginalMinY()
	{
		return minY;
	}
	
	public double getOriginalMinZ()
	{
		return minZ;
	}
	
	public PrismImporter(String name)
	{
		try
		{
			scan = new Scanner(new File((name + ".obj")));
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		while(scan.hasNext())
		{
			String curLine = scan.nextLine();
			if(curLine.length() > 2)
			{
				int curChar = 2;
				if(curLine.substring(0, 2).equals("v "))
				{
					vertexList.add(new double[3]);
					String coordX = "", coordY = "", coordZ = "";
					while(curLine.charAt(curChar) != ' ')
					{
						coordX += curLine.charAt(curChar);
						curChar++;
					}
					double curX = Double.parseDouble(coordX);
					vertexList.get(vertexList.size() - 1)[0] = curX;
					curChar++;
					while(curLine.charAt(curChar) != ' ')
					{
						coordY += curLine.charAt(curChar);
						curChar++;
					}
					double curY = Double.parseDouble(coordY);
					vertexList.get(vertexList.size() - 1)[1] = curY;
					curChar++;
					while(curChar < curLine.length())
					{
						coordZ += curLine.charAt(curChar);
						curChar++;
					}
					double curZ = Double.parseDouble(coordZ);
					vertexList.get(vertexList.size() - 1)[2] = curZ;
					
					if(curX < minX) minX = curX;
					else if(curX > greatestX) greatestX = curX;
					if(curY < minY) minY = curY;
					else if(curY > greatestY) greatestY = curY;
					if(curZ < minZ) minZ = curZ;
					else if(curZ > greatestZ) greatestZ = curZ;
				}
				else if(curLine.substring(0, 3).equals("vt "))
				{
					curChar++;
					textureList.add(new double[2]);
					String coordX = "", coordY = "";
					while(curLine.charAt(curChar) != ' ')
					{
						coordX += curLine.charAt(curChar);
						curChar++;
					}
					textureList.get(textureList.size() - 1)[0] = Double.parseDouble(coordX);
					while(curChar < curLine.length())
					{
						coordY += curLine.charAt(curChar);
						curChar++;
					}
					textureList.get(textureList.size() - 1)[1] = Double.parseDouble(coordY);
				}
				else if(curLine.substring(0, 2).equals("f "))
				{
					String firstCoords = "", secondCoords = "", thirdCoords = "";
					String firstTex = "", secondTex = "", thirdTex = "";
					int xyz = 0;
					int vertexTextureNormal = 0;
					orderedVertices.add(new double[9]);
					orderedTextures.add(new double[6]);
					for(int i = 2; i < curLine.length(); i++)
					{
						if(curLine.charAt(i) == '/' || curLine.charAt(i) == ' ')
						{
							vertexTextureNormal++;
							if(vertexTextureNormal == 3)
							{
								vertexTextureNormal = 0;
							}
							if(curLine.charAt(i) == ' ')
							{
								xyz++;
								vertexTextureNormal = 0;
							}
						}
						else if(vertexTextureNormal == 0)
						{
							if(xyz == 0)
							{
								firstCoords += curLine.charAt(i);
							}
							else if(xyz == 1)
							{
								secondCoords += curLine.charAt(i);
							}
							else if(xyz == 2)
							{
								thirdCoords += curLine.charAt(i);
							}
						}
						else if(vertexTextureNormal == 1)
						{
							if(xyz == 0)
							{
								firstTex += curLine.charAt(i);
							}
							else if(xyz == 1)
							{
								secondTex += curLine.charAt(i);
							}
							else if(xyz == 2)
							{
								thirdTex += curLine.charAt(i);
							}
						}
					}

					for(int i = 0; i < 3; i++)
					{
						orderedVertices.get(orderedVertices.size() - 1)[i] = vertexList.get(Integer.parseInt(firstCoords) - 1)[i];
					}
					for(int i = 3; i < 6; i++)
					{
						orderedVertices.get(orderedVertices.size() - 1)[i] = vertexList.get(Integer.parseInt(secondCoords) - 1)[i - 3];
					}
					for(int i = 6; i < 9; i++)
					{
						orderedVertices.get(orderedVertices.size() - 1)[i] = vertexList.get(Integer.parseInt(thirdCoords) - 1)[i - 6];
					}
					for(int i = 0; i < 2; i++)
					{
						orderedTextures.get(orderedVertices.size() - 1)[i] = textureList.get(Integer.parseInt(firstTex) - 1)[i];
					}
					for(int i = 2; i < 4; i++)
					{
						orderedTextures.get(orderedVertices.size() - 1)[i] = textureList.get(Integer.parseInt(secondTex) - 1)[i - 2];
					}
					for(int i = 4; i < 6; i++)
					{
						orderedTextures.get(orderedVertices.size() - 1)[i] = textureList.get(Integer.parseInt(thirdTex) - 1)[i - 4];
					}
				}
			}
		}
		sizeX = greatestX - minX;
		sizeY = greatestY - minY;
		sizeZ = greatestZ - minZ;
	}

	public PrismImporter(String name, Prism offset)
	{
		try
		{
			scan = new Scanner(new File((name + ".obj")));
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		while(scan.hasNext())
		{
			String curLine = scan.nextLine();
			if(curLine.length() > 2)
			{
				int curChar = 2;
				if(curLine.substring(0, 2).equals("v "))
				{
					vertexList.add(new double[3]);
					String coordX = "", coordY = "", coordZ = "";
					while(curLine.charAt(curChar) != ' ')
					{
						coordX += curLine.charAt(curChar);
						curChar++;
					}
					double curX = Double.parseDouble(coordX) * offset.getSizeX() + offset.getX();
					vertexList.get(vertexList.size() - 1)[0] = curX;
					curChar++;
					while(curLine.charAt(curChar) != ' ')
					{
						coordY += curLine.charAt(curChar);
						curChar++;
					}
					double curY = Double.parseDouble(coordY) * offset.getSizeY() + offset.getY();
					vertexList.get(vertexList.size() - 1)[1] = curY;
					curChar++;
					while(curChar < curLine.length())
					{
						coordZ += curLine.charAt(curChar);
						curChar++;
					}
					double curZ = Double.parseDouble(coordZ) * offset.getSizeZ() + offset.getZ();
					vertexList.get(vertexList.size() - 1)[2] = curZ;
					
					if(curX < minX) minX = curX;
					else if(curX > greatestX) greatestX = curX;
					if(curY < minY) minY = curY;
					else if(curY > greatestY) greatestY = curY;
					if(curZ < minZ) minZ = curZ;
					else if(curZ > greatestZ) greatestZ = curZ;
				}
				else if(curLine.substring(0, 3).equals("vt "))
				{
					curChar++;
					textureList.add(new double[2]);
					String coordX = "", coordY = "";
					while(curLine.charAt(curChar) != ' ')
					{
						coordX += curLine.charAt(curChar);
						curChar++;
					}
					textureList.get(textureList.size() - 1)[0] = Double.parseDouble(coordX);
					while(curChar < curLine.length())
					{
						coordY += curLine.charAt(curChar);
						curChar++;
					}
					textureList.get(textureList.size() - 1)[1] = Double.parseDouble(coordY);
				}
				else if(curLine.substring(0, 2).equals("f "))
				{
					String firstCoords = "", secondCoords = "", thirdCoords = "";
					String firstTex = "", secondTex = "", thirdTex = "";
					int xyz = 0;
					int vertexTextureNormal = 0;
					orderedVertices.add(new double[9]);
					orderedTextures.add(new double[6]);
					for(int i = 2; i < curLine.length(); i++)
					{
						if(curLine.charAt(i) == '/' || curLine.charAt(i) == ' ')
						{
							vertexTextureNormal++;
							if(vertexTextureNormal == 3)
							{
								vertexTextureNormal = 0;
							}
							if(curLine.charAt(i) == ' ')
							{
								xyz++;
								vertexTextureNormal = 0;
							}
						}
						else if(vertexTextureNormal == 0)
						{
							if(xyz == 0)
							{
								firstCoords += curLine.charAt(i);
							}
							else if(xyz == 1)
							{
								secondCoords += curLine.charAt(i);
							}
							else if(xyz == 2)
							{
								thirdCoords += curLine.charAt(i);
							}
						}
						else if(vertexTextureNormal == 1)
						{
							if(xyz == 0)
							{
								firstTex += curLine.charAt(i);
							}
							else if(xyz == 1)
							{
								secondTex += curLine.charAt(i);
							}
							else if(xyz == 2)
							{
								thirdTex += curLine.charAt(i);
							}
						}
					}

					for(int i = 0; i < 3; i++)
					{
						orderedVertices.get(orderedVertices.size() - 1)[i] = vertexList.get(Integer.parseInt(firstCoords) - 1)[i];
					}
					for(int i = 3; i < 6; i++)
					{
						orderedVertices.get(orderedVertices.size() - 1)[i] = vertexList.get(Integer.parseInt(secondCoords) - 1)[i - 3];
					}
					for(int i = 6; i < 9; i++)
					{
						orderedVertices.get(orderedVertices.size() - 1)[i] = vertexList.get(Integer.parseInt(thirdCoords) - 1)[i - 6];
					}
					for(int i = 0; i < 2; i++)
					{
						orderedTextures.get(orderedVertices.size() - 1)[i] = textureList.get(Integer.parseInt(firstTex) - 1)[i];
					}
					for(int i = 2; i < 4; i++)
					{
						orderedTextures.get(orderedVertices.size() - 1)[i] = textureList.get(Integer.parseInt(secondTex) - 1)[i - 2];
					}
					for(int i = 4; i < 6; i++)
					{
						orderedTextures.get(orderedVertices.size() - 1)[i] = textureList.get(Integer.parseInt(thirdTex) - 1)[i - 4];
					}
				}
			}
		}
		sizeX = greatestX - minX;
		sizeY = greatestY - minY;
		sizeZ = greatestZ - minZ;
	}
}